﻿@{
    Layout = null;
}
@using CNKI.TPI.Web.Search.Model;
@using CNKI.TPI.Web.Base;
@using CNKI.TPI.Web.Admin.Model;
<script>
    function mapingDel(SysID) {
        var urlstr = "@Html.Raw(Url.Action("MappingDel", "Admin_MultiDBPublish"))";
        var dataobj = { "SysID": SysID };
        confirmDialog('确定要删除吗?', function () {
            ajaxfunc(urlstr, dataobj);
        })
    }
    function mapingUpdate(SysID, usfield, dbfield) {

        $("#DbField").val(dbfield);
        $("#UsField").val(usfield);
        $("#DbField").attr("disabled", "disabled");
        $("#sysMapingID").val(SysID);
        $(".talkDivBg").show();
        $(".form-h-8em").show();

    }
    function ajaxfunc(urlstr, datainfo) {
        $.ajax({
            type: "GET",
            url: timestamp(urlstr),
            data: datainfo,
            success: function (msg) {
                if (msg == "True") {
                    messageAlert("操作成功！");
                    var urlstr = "@Html.Raw(Url.Action("MappingInfo", "Admin_MultiDBPublish"))";
                    var dataobj = { "DbID": $("#DbID").val(), "DbCode": $("#DbCode").val() };
                    ajaxLoad("initload", urlstr, dataobj);
                }
                else {
                    messageAlert("操作失败！");
                }

            },
            error: function (e)
            { }
        })
    }
    function addMapping() {

        $("#DbField").removeAttrs("disabled", "disabled");
        $("#DbField").get(0).selectedIndex = 0;
        $("#UsField").get(0).selectedIndex = 0;
        $(".talkDivBg").show();
        $(".form-h-8em").show();
    }
    function closeMapping() {
        $(".talkDivBg").hide();
        $(".form-h-8em").hide();
    }
    function saveMapping() {
        var flage = "add";
        var sysmid = "0";
        if ($("#DbField").attr("disabled") == "disabled") //修改
        {
            flage = "update";
            sysmid = $("#sysMapingID").val(); //SYS_CMS_US_FIELDMAP映射表主键ID
        }
        var dbid = $("#DbID").val();
        var dbfieldid = $("#DbField").val();
        var usfieldid = $("#UsField").val();
        $.ajax({
            type: "POST",
            url: "@Html.Raw(Url.Action("MappingSave", "Admin_MultiDBPublish"))",
            data: { "dbfieldID": dbfieldid, "usfield": usfieldid, "DbID": dbid, "flage": flage, "sysid": sysmid },
            success: function (msg) {
                if (msg == "1") {
                    closeMapping();
                    messageAlert("操作成功！");
                    var urlstr = "@Html.Raw(Url.Action("MappingInfo", "Admin_MultiDBPublish"))";
                    var dataobj = { "DbID": dbid, "DbCode": $("#DbCode").val() };
                    ajaxLoad("initload", timestamp(urlstr), dataobj);
                }
                else if (msg == "2") {
                    messageAlert("该公共字段已经设置过映射!");
                }
                else if (msg == "3") {
                    messageAlert("该数据库字段已经设置过映射!");
                }
                else {
                    messageAlert("操作失败！");
                }

            },
            error: function (e)
            { }
        })
      
    }
</script>
<input  value="@ViewBag.DbID"  id="DbID" type="hidden"/>
<input value="@ViewBag.DbCode"  id="DbCode" type="hidden"/>
 <h3 class='box-h' >单库映射设置</h3>
<div class="wrapR-c" style="padding-top: 20px">
       
    <div class='posr' style="padding-bottom: 20px">
        <a class="btn btn-m btn-defa-g" href="javascript:void(0);" onclick="addMapping()">添加映射</a>
        <a class="btn btn-m btn-prim-f" href="javascript:void(0);" onclick="ajaxLoad('initload', '@Html.Raw(Url.Action("DBMapping", "Admin_MultiDBPublish"))');">返回</a>
    </div>

    <table width="100%" cellspacing="0" cellpadding="0" border="0" class="table table-grey">
        <thead>
            <tr class="thead">
                <th class="tdnum">序号</th>
                <th class=''>数据库字段</th>
                <th class='tl'>公共字段</th>
                <th>映射结果</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            @{if (ViewBag.DbFieldList != null)
              {
                  int sortno = 1;
                  foreach (SYS_CMS_US_FIELDMAP item in ViewBag.DbFieldList)
                  {

                      string str = "even";
                      if (sortno % 2 == 0)
                      {
                          str = "odd";
                      }
                <tr class='@str'>
                    <td>@sortno</td>
                    <td>@Html.Raw(item.DBFieldName)</td>
                    <td>@Html.Raw(item.USFieldName)</td>
                    <td class='tc'>
                        @Html.Raw(item.DBFieldName)--@Html.Raw(item.USFieldName)
                    </td>
                    <td class="tc">
                        <a onclick="mapingUpdate('@item.SysID','@item.USFieldID','@item.FieldID')" href="javascript:void(0)">编辑</a>
                        <a onclick="mapingDel('@item.SysID')" href="javascript:void(0)">删除</a>
                    </td>
                </tr>
                      sortno = sortno + 1;
                  }
              }

            }
        </tbody>
    </table>

</div>
  <div class="talkDivBg" style="display: none;">
    <!--talkDiv弹出层(不带图片，宽550)-->
    <div class="talkDiv talkW-550 form-h form-h-8em" style="display: none">
        <div class="talkDiv-title">
            <span>映射设置</span>
            <a class="icon icon-close" title="关闭" href="javascript:void(0);" onclick="closeMapping()"><i class="iconfont icon-error3"></i></a>
        </div>
        <div class="talkDiv-body">

            <div class="f-gp">
                <div class="f-lb">
                    <label class="f-label">数据库字段：</label>
                </div>
                <div class="f-ct">
                    <select class="f-select span4" name="DbField" id="DbField">
                        @foreach (SYS_CMS_DATABASE_FIELD item in ViewBag.DbSelfField)
                    {
                            <option value="@item.SysID">@item.Field</option>
                    }
                    </select>
                </div>
            </div>

            <div class="f-gp">
                <div class="f-lb">
                    <label class="f-label">公共字段：</label>
                </div>
                <div class="f-ct">
                    <select class="f-select span4" name="UsField" id="UsField">
                        @foreach (SYS_CMS_US_FIELD item in ViewBag.DbUSField)
                    {
                            <option value="@item.SYSID">@item.Name</option>
                    }
                    </select>

                </div>
            </div>

        </div>
        <input type="hidden" value="0" id="sysMapingID" />
        <div class="talkDiv-btn"><a href="javascript:void(0);" class="btn btn-m btn-prim-f btn-submit" onclick="saveMapping()">保存</a> &#12288;</div>
    </div>
</div>
